﻿Shader "Example01/13通道流光UI"
{
    Properties
    {
        _MainCol("MainCol",Color)= (1,1,1,1)
        [PerRendererData] _MainTex ("Texture", 2D) = "white" {} // Image组件的图集 就是 _MainTex 属性
        _Fluxay ("Fluxay", 2D) = "black" {}
        _Speed("Speed",float) = 1

        //--- 层级处理所需 ------------------------------
        [PerRendererData] _StencilComp ("Stencil Comparison", Float) = 8
        [PerRendererData] _Stencil ("Stencil ID", Float) = 0
        [PerRendererData] _StencilOp ("Stencil Operation", Float) = 0
        [PerRendererData] _StencilWriteMask ("Stencil Write Mask", Float) = 255
        [PerRendererData] _StencilReadMask ("Stencil Read Mask", Float) = 255
        [PerRendererData] _ColorMask ("Color Mask", Float) = 15
    }
    SubShader
    {
        Tags
        {
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "RenderType"="Transparent"
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
        }

        Stencil
        {
            Ref [_Stencil]
            Comp [_StencilComp]
            Pass [_StencilOp]
            ReadMask [_StencilReadMask]
            WriteMask [_StencilWriteMask]
        }
        
        Cull Off
        Lighting Off
        ZWrite Off
        ZTest [unity_GUIZTestMode]
        Blend SrcAlpha OneMinusSrcAlpha
        ColorMask [_ColorMask]
        
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            #include "UnityUI.cginc"    // 引入UI.cginc文件



            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float4 vertex : SV_POSITION;
                float2 uv : TEXCOORD0;
                float2 uv2 : TEXCOORD1;
                float4 worldPos : TEXCOORD2;
            };

            float4 _MainCol;
            sampler2D _MainTex;
            float4 _MainTex_ST;
            float _Speed;
            sampler2D _Fluxay;
            float4 _Fluxay_ST;

            float4 _ClipRect; // 2DMask 支持

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv =TRANSFORM_TEX(v.uv,_MainTex);
                o.uv2 =TRANSFORM_TEX(v.uv,_Fluxay);
                o.uv2.x -= _Time.x * _Speed;
                o.worldPos = v.vertex;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                fixed mask = tex2D(_Fluxay, i.uv).r;
                fixed fluxay = tex2D(_Fluxay, i.uv2).g;
                col.rgb += fluxay * _MainCol.rgb * mask * _MainCol.a;

                col.a *= UnityGet2DClipping(i.worldPos.xy, _ClipRect);  // 2DMask 支持

                return col;
            }
            ENDCG
        }
    }
}